<template>
	<view>
		<!--表单部分-->
		<form>
			<!--头像-->
			<view class="cu-form-group margin-top">
				<view class="title">头像</view>
				<view class="cu-avatar radius bg-gray"  >
					<image class="user-avatar" :src='user.avatarurl' @tap="showModal" data-target="bottomModal"></image>
				</view>
			</view>
			<!--头像-->
			
			<!--弹窗-->
			<view class="cu-modal bottom-modal" :class="modalName=='bottomModal'?'show':''">
				<view class="cu-dialog">
					<view class="cu-bar bg-white">
						<view class="action text-blue" @tap="hideModal">取消</view>
						<view class="action text-green" @tap="changeImg">确定</view>
					</view>
					
					<view class="cu-form-group">
						<view class="grid col-4 grid-square flex-sub">
							<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
							 <image :src="imgList[index]" mode="aspectFill"></image>
								<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
									<text class='cuIcon-close'></text>
								</view>
							</view>
							<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
								<text class='cuIcon-cameraadd'></text>
							</view>
						</view>
					</view>
					
				</view>
			</view>
			<!--弹窗-->
			
			<!--性别-->
			<!--组件未选中时背景颜色暂时不能改变-->
			<view class="cu-form-group">
				<view class="title">您的性别</view>
				<switch style="font-size: 110%;" class='switch-sex red' @change="SwitchSex" :class="switchSex?'checked':''" :checked="switchSex?true:false"></switch>
			</view>
			<!--性别-->

			<!--昵称-->
			<view class="cu-form-group">
				<view class="title">您的昵称</view>
				<input v-model="user.nickname" :placeholder='user.nickname' name="input"></input>
			</view>
			<!--昵称-->
			
			<!--手机号码
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input v-model="user.phone_number" :placeholder="user.phone_number" name="input"></input>
				<view class="cu-capsule radius">
					<view class='cu-tag bg-blue '>
						+86		
					</view>
					<view class="cu-tag line-blue">
						中国大陆
					</view>
				</view>
			</view>
			手机号码-->
			
			<!--地址-->
				<!--所在地区-->
				<view class="cu-form-group"> 
					<view class="title align-center" >您的地区</view>
					<text class="cuIcon-right align-center" @click="getAddress"></text>
				</view>
			<!--地址-->	
			
			

		</form>
		<!--表单-->
		
		<!--提交按钮-->
				<view class="padding flex flex-direction">
					<button class="cu-btn bg-green lg" @click="updateUser">提交</button>
				</view>
		<!--提交按钮-->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				switchSex:false,
				modalName: null,
				imgList: [],
				//用户数据
				//name：用户昵称
				//image：用户头像
				//id：用户id
				//phone_number：用户手机号码
				//location1：用户所在地区
				//location2：用户具体地址
				//email：用户电子邮箱
				//switchSex：用户性别
				user:{
					
				},
				
			}
		},
		onLoad() {
			this.getUser();
		},
		methods: {
			//地区选择器
			getAddress(){
				uni.navigateTo({
					url:"../mine-areas/mine-areas"
				})
			},
			
			//性别更改
			SwitchSex(e){
				this.switchSex=e.detail.value;
			},
			
			//底部窗口
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			
			//
			hideModal(e){
				this.modalName = null ;
				this.imgList.splice(0);
			},
			
			//
			changeImg(e){
				if(this.imgList[0]==null){
					this.modalName = null;
					return;
				}
				this.user.avatarurl = this.imgList[0];
				this.imgList.splice(0);
				this.modalName = null;
				this.uploadAvatarurl();
				
			},
			
			//
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
							this.user.avatarurl = this.imgList[0]
						} else {
							this.imgList = res.tempFilePaths
							this.user.avatarurl = this.imgList[0]
						}
					}
				});
			},
			
			//
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
				this.user.avatarurl = e.currentTarget.dataset.url
			},
			
			//
			DelImg(e) {
				uni.showModal({
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			uploadAvatarurl(){
				var that = this
				console.log(this.user.avatarurl);
				uni.uploadFile({
					url:this.apiServer + "/upload/fileUpload",
					filePath:this.user.avatarurl,
					name:'file',
					formData:{
						"type":"img"
					},
					success:function(res){
						console.log(res.data);
						that.user.avatarurl = res.data;
					}
				});
			},
			getUser(){
				this.user=uni.getStorageSync("user");
			},
			updateUser(){
				console.log(this.user.avatarurl);
				var id = this.user.id;
				var avatarurl = this.user.avatarurl;
				var nickname = this.user.nickname;
				this.request({
					url: this.apiServer + "/user/updateUser",
					method: "PUT",
					data:{
						"id":id,
						"avatarurl":avatarurl,
						"nickname":nickname,
					},
					header:{
						"Content-Type": "application/x-www-form-urlencoded"
					},
				}).then(res => {
					console.log(res);
					this.upgradeUser();
				});
				uni.navigateBack();
			},
			upgradeUser(){
				this.request({
					url:this.apiServer + "/user/findUserById?id="+this.user.id
				}).then(res => {
					uni.setStorageSync("user",res.data);
				})
			},
			changeRegion(e){
				this.region = e.detail.value;
			}
		}
	}
</script>

<style>
.user-logo{
	border-radius: 50%;
	display: flex;
	align-items: center;
	width: 150upx;
	height: 150upx;
}
.user-avatar{
	width: 60upx;
	height: 60upx;
	display: flex;
	align-items: center;
}
.container{
	display: flex;
	align-items: center;
}
.user-area{
	font-size: 110%;
	display:flex;
	align-items: center;
	right: 100upx;
}
</style>